import collections


class Solution(object):
    def longestRepeatingSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        n = len(s)
        for k in range(n - 1, 0, -1):
            d = collections.defaultdict(int)
            for i in range(n - k + 1):
                d[s[i: i + k]] += 1
                if d[s[i: i + k]] >= 2:
                    return k
        return 0



data = Solution()
s = "abbaba"
print(data.longestRepeatingSubstring(s))
s = "aaabaabbbaaabaabbaabbbabbbaaaabbaaaaaabbbaabbbbbbbbbaaaabbabbaba"
print(data.longestRepeatingSubstring(s))